package DataEncryptionStandard;

import org.apache.commons.codec.binary.Hex;

public class DESTest {
    private static final DES des = new DES();

    public static void main(String[] args) throws Exception {
        DESTest desTest = new DESTest();
//        desTest.middleTest();
        desTest.primaryTest();

    }

    //使用书上的中间数据来进行代码调试
    private void middleTest() throws Exception {
        byte[] textBytes = Hex.decodeHex("02468aceeca86420");
        BitStream textStream = new BitStream(textBytes.length * 8, textBytes);

        byte[] keyBytes = Hex.decodeHex("0f1571c947d9e859");

        BitStream encryptData = des.encrypt(textStream, des.generateKey(keyBytes));
        System.out.println(encryptData);
    }

    private void setBitTest() throws Exception {
        byte[] bytes = new byte[2];
        bytes[0] = 0B01100000;
        bytes[1] = (byte) 0B11110000;
        BitStream bitStream = new BitStream(15, bytes);
        System.out.println(bitStream);
        bitStream.set(1, 0);
        System.out.println(bitStream);
    }

    private void primaryTest() throws Exception {
        DesTestData[] testData = new DesTestData[]{
                // 以下两个是正常密钥的测试数据
                new DesTestData("0f1571c947d9e859", "02468aceeca86420", "da02ce3a89ecac3b"),
                new DesTestData("fedcba9876543210", "0123456789abcdef", "ed39d950fa74bcc4"),
                // 以下几个是弱密钥
                new DesTestData("0101010101010101", "0123456789abcdef", "617b3a0ce8f07100"),
                new DesTestData("fefefefefefefefe", "0123456789abcdef", "6dce0dc9006556a3"),
                new DesTestData("e0e0e0e0f1f1f1f1", "0123456789abcdef", "ee600bc06fc9ef23"),
                new DesTestData("1f1f1f1f0e0e0e0e", "0123456789abcdef", "db958605f8c8c606"),
                new DesTestData("0000000000000000", "0123456789abcdef", "617b3a0ce8f07100"),
                new DesTestData("ffffffffffffffff", "0123456789abcdef", "6dce0dc9006556a3"),
                new DesTestData("e1e1e1e1f0f0f0f0", "0123456789abcdef", "ee600bc06fc9ef23"),
                new DesTestData("1e1e1e1e0f0f0f0f", "0123456789abcdef", "db958605f8c8c606"),
                // 以下几个是半弱密钥
                new DesTestData("011f011f010e010e", "0123456789abcdef", "6f2c1f78866ccf13"),
                new DesTestData("01e001e001f101f1", "0123456789abcdef", "35c368269fefe0df"),
                new DesTestData("01fe01fe01fe01fe", "0123456789abcdef", "8a76c7a4f16d47ed"),
                new DesTestData("1fe01fe00ef10ef1", "0123456789abcdef", "7ccf66359f9dfc11"),
                new DesTestData("1ffe1ffe0efe0efe", "0123456789abcdef", "7cf3daade2867199"),
                new DesTestData("e0fee0fef1fef1fe", "0123456789abcdef", "edbfd1c66c29ccc7"),
        };

        boolean flag = true;
        for (int i = 0; i < testData.length; i++) {
            BitStream encryptRes = des.encrypt(testData[i].getPlaintext(), testData[i].getKey());
            if (!testData[i].getCiphertext().equals(encryptRes)) {
                flag = false;
            }

            BitStream decryptRes = des.decrypt(encryptRes, testData[i].getKey());
            if (!decryptRes.equals(testData[i].getPlaintext())) {
                flag = false;
            }
        }

        if (flag) {
            System.out.println("DES.primaryTest：DES加解密测试通过");
        } else {
            System.out.println("DES.primaryTest：DES加解密测试不通过");
        }
    }
}
